﻿<%@ WebHandler Language="C#" Class="SIPO.Web.AjaxHandler.Transaction.recCalculationhandler" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

using SIPO.Library.DataAccess.Transaction;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Administration;
using SIPO.Library.DomainObject.Administration;

using SIPO.Library.Enum;
using SIPO.Web.Support;

using Newtonsoft.Json.Linq;
namespace SIPO.Web.AjaxHandler.Transaction
{

    public class recCalculationhandler : SIPO.Web.BaseHandler
    {
        private recCalculationRepo recCalculationDal = new recCalculationRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        private recLogRepo logDal = new recLogRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
        private string MethodName = string.Empty;
        public override void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            MethodName = context.Request.Params["method"];

            context.Response.Clear();
            context.Response.Buffer = true;

            switch (MethodName.ToLower())
            {
                case ("insert"):
                    context.Response.Write(Insert(context));
                    break;
                case ("getdata"):
                    context.Response.Write(GetData(context));
                    break;
                default:
                    // do nothing
                    break;
            }

        }

        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private string Insert(HttpContext context)
        {
            JSONHelper helper = new JSONHelper();
            try
            {
                recCalculationDal.Save(base.UserLoggedIn);//run [dbo].[SP_GenPeriodicDues]

                helper.Message = "Perhitungan selesai !";
                helper.IsSuccess = true;
            }
            catch
            {
                helper.IsSuccess = false;
                helper.Message = "Perhitungan gagal !";
            }
            finally
            {
                recLog log = new recLog();
                log.LogEvent = "Insert";
                log.LogMessage = helper.Message;
                log.LogSource = "recCalculationhandler.ashx";
                log.CreatedBy = base.UserLoggedIn;
                log.CreatedDate = DateTime.Now;
                logDal.Save(log);
            }
            return Newtonsoft.Json.JsonConvert.SerializeObject(helper);
        }

        private string GetData(HttpContext context)
        {
            string parameters = Helper.JqGridFilterToSQLParams(context);
            string yeardues = context.Request.Params["yearreport"].ToString();
            int debtorid = Convert.ToInt32(context.Request.Params["debtorid"].ToString());
            int debtorsectorid = Convert.ToInt32(context.Request.Params["debtorsectorid"].ToString());
            parameters += "WHERE YearReport=" + yeardues + " AND transactions.recImposition.DebtorSectorID=" + debtorsectorid + " AND transactions.recBilling.Value <> 0";

            int page = 1;
            int.TryParse(context.Request.QueryString["page"] ?? "1", out page);

            int pageSize = 10;
            int.TryParse(context.Request.QueryString["rows"] ?? "10", out pageSize);

            string sidx = ((context.Request.QueryString["sidx"]) == null ? recCalculation.Fields.CalculationID : context.Request.QueryString["sidx"].ToString());

            string sord = ((context.Request.QueryString["sord"]) == null ? "ASC" : context.Request.QueryString["sord"].ToString());

            int totalCount = 0;
            int totalPages = 0;
            List<recCalculation> pagedList = null;

            try
            {
                totalCount = Convert.ToInt32(recCalculationDal.SelectScalar(SQL.Function.Aggregate.Count, "recCalculation.CalculationID", parameters));

                decimal dc = decimal.Ceiling(Convert.ToDecimal(totalCount) / Convert.ToDecimal(pageSize));
                totalPages = Convert.ToInt32(dc);

                pagedList = recCalculationDal.GetList(pageSize, page, sidx, sord, parameters);
            }
            catch (Exception ex)
            {
                recLog log = new recLog();
                log.LogEvent = "GetData";
                log.LogMessage = ex.Message;
                log.LogSource = "recCalculationhandler.ashx";
                log.CreatedBy = base.UserLoggedIn;
                log.CreatedDate = DateTime.Now;
                logDal.Save(log);
                log.Dispose();
            }
            finally
            {

            }

            // send params into jqGrid's format
            var gridData = new
            {
                page = page,
                total = totalPages,
                records = totalCount,
                rows = pagedList,
            };

            return Newtonsoft.Json.JsonConvert.SerializeObject(gridData);

        }

    }
}